package com.gxljc.bear.crawler.base;

import com.gxljc.commons.config.ConfigUtil;
import com.gxljc.commons.util.config.CommConsts;
import org.apache.hadoop.conf.Configuration;
import org.apache.log4j.Logger;

import java.io.Serializable;

public class DolphinCrawlerConf extends Configuration implements Serializable {
    private static final long serialVersionUID = 1L;

    private static Logger LOG = Logger.getLogger(DolphinCrawlerConf.class);

    private static final String[] CONF_COMM_PARAMS = {CommConsts.REDIS_SERVER,
            CommConsts.ES_CLUSTER_NAME, CommConsts.ES_HOSTS,
            CommConsts.SPARK_MASTER_URL,
            DolphinCrawlerConsts.PARAM_MONGO_BASE_URL,
            DolphinCrawlerConsts.PARAM_PLATFORM_NAME,
            DolphinCrawlerConsts.PARAM_DETECTOR_WEB_HOST
            };
    private static final String[] CONF_COMM_SERV = {};

    public static final String[] CONF_APP_PARAMS = new String[]{
            DolphinCrawlerConsts.PARAM_MYSQL_URL,
            DolphinCrawlerConsts.PARAM_MYSQL_ANALgxljc_URL,
			DolphinCrawlerConsts.PARAM_IMAGEWEB_EXPLAIN_DOMAIN,
            DolphinCrawlerConsts.PARAM_IMAGEWEB_RESOURCE_DOMAIN,
            DolphinCrawlerConsts.QUERY_RPC_PORT
    };
    public static final String[] CONF_APP_SERV = new String[]{};

    private static final DolphinCrawlerConf config = new DolphinCrawlerConf();

    private DolphinCrawlerConf() {
        LOG.info("add conf resource");
        initConfiguration(this);
        try {
            LOG.info("load common config");
            ConfigUtil.fastLoadConfig(this, CommConsts.ORG, CommConsts.APP,
                    CONF_COMM_PARAMS, CONF_COMM_SERV);
            LOG.info("load dolphin config");
            ConfigUtil.fastLoadConfig(this, DolphinCrawlerConsts.ORG,
                    DolphinCrawlerConsts.APP, CONF_APP_PARAMS, CONF_APP_SERV);
            LOG.info("load all config success");
        } catch (Exception e) {
            LOG.fatal("load config error: ", e);
            LOG.fatal("Process exit.");
            System.exit(0);
        }
    }

    public static DolphinCrawlerConf getInstance() {
        return config;
    }

    private static void initConfiguration(Configuration conf) {
        conf.addResource(DolphinCrawlerConsts.FILE_HBASE_CONFIG);
        conf.addResource(DolphinCrawlerConsts.FILE_CORE_SITE);
        conf.addResource(DolphinCrawlerConsts.FILE_HDFS_SITE);
        conf.addResource(DolphinCrawlerConsts.FILE_YARN_SITE);
        conf.addResource(DolphinCrawlerConsts.FILE_DOLPHIN_CRAWLER);
    }
}
